package com.deer.wms.kuaidiniao.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deer.wms.kuaidiniao.dao.KuaidiniaoMapper;
import com.deer.wms.kuaidiniao.model.*;
import com.deer.wms.kuaidiniao.service.KuaidiniaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by ll on 2020/02/06.
 */
@Service
@Transactional
public class KuaidiniaoServiceImpl extends ServiceImpl<KuaidiniaoMapper, Kuaidiniao> implements KuaidiniaoService {

    @Autowired
    private EOrderApi eOrderApi;
    @Autowired
    private JscxApi jscxApi;
    @Autowired
    private WlgzApi wlgzApi;
    @Autowired
    private ZtjkApi ztjkApi;
    @Autowired
    private CloudPrintApi cloudPrintApi;

    @Override
    public String dzmd(EOrderRequestData requestData) {
        String str = "";
        try {
            str = eOrderApi.orderOnlineByRequestData(requestData);
            System.out.println("返回信息：：：：：" + str);
        } catch (Exception ec) {
            System.out.println(ec.getStackTrace());
        }
        return str;
    }

    @Override
    public List<Trace> jscx(String expCode, String expNo) {
        List<Trace> traces = new ArrayList<>();
        try {
            String str = jscxApi.getOrderTracesByJson(expCode, expNo);
            System.out.println("返回信息：：：：：" + str);

            str = "{\n" +
                    "    \"EBusinessID\": \"1109259\",\n" +
                    "    \"OrderCode\": \"\",\n" +
                    "    \"ShipperCode\": \"SF\",\n" +
                    "    \"LogisticCode\": \"118461988807\",\n" +
                    "    \"Success\": true,\n" +
                    "    \"State\": 3,\n" +
                    "    \"Reason\": null,\n" +
                    "    \"Traces\": [\n" +
                    "        {\n" +
                    "            \"AcceptTime\": \"2014/06/25 08:05:37\",\n" +
                    "            \"AcceptStation\": \"正在派件..(派件人:邓裕富,电话:18718866310)[深圳 市]\",\n" +
                    "            \"Remark\": null\n" +
                    "        },\n" +
                    "        {\n" +
                    "            \"AcceptTime\": \"2014/06/25 04:01:28\",\n" +
                    "            \"AcceptStation\": \"快件在 深圳集散中心 ,准备送往下一站 深圳 [深圳市]\",\n" +
                    "            \"Remark\": null\n" +
                    "        },\n" +
                    "        {\n" +
                    "            \"AcceptTime\": \"2014/06/25 01:41:06\",\n" +
                    "            \"AcceptStation\": \"快件在 深圳集散中心 [深圳市]\",\n" +
                    "            \"Remark\": null\n" +
                    "        },\n" +
                    "        {\n" +
                    "            \"AcceptTime\": \"2014/06/24 20:18:58\",\n" +
                    "            \"AcceptStation\": \"已收件[深圳市]\",\n" +
                    "            \"Remark\": null\n" +
                    "        },\n" +
                    "        {\n" +
                    "            \"AcceptTime\": \"2014/06/24 20:55:28\",\n" +
                    "            \"AcceptStation\": \"快件在 深圳 ,准备送往下一站 深圳集散中心 [深圳市]\",\n" +
                    "            \"Remark\": null\n" +
                    "        },\n" +
                    "        {\n" +
                    "            \"AcceptTime\": \"2014/06/25 10:23:03\",\n" +
                    "            \"AcceptStation\": \"派件已签收[深圳市]\",\n" +
                    "            \"Remark\": null\n" +
                    "        },\n" +
                    "        {\n" +
                    "            \"AcceptTime\": \"2014/06/25 10:23:03\",\n" +
                    "            \"AcceptStation\": \"签收人是：已签收[深圳市]\",\n" +
                    "            \"Remark\": null\n" +
                    "        }\n" +
                    "    ]\n" +
                    "}";
            JSONObject jsonObject = JSONObject.parseObject(str);
            String jsonArray = jsonObject.getString("Traces");
            traces = JSONObject.parseArray(jsonArray, Trace.class);
            System.out.println(traces);


        } catch (Exception ec) {
            System.out.println(ec.getStackTrace());
        }
        return traces;
    }

    @Override
    public String wlgz(WlgzRequestData requestData) {
        String str = "";
        try {
            str = wlgzApi.orderTraceSubByRequestData(requestData);
            System.out.println("返回信息：：：：：" + str);
        } catch (Exception ec) {
            System.out.println(ec.getStackTrace());
        }
        return str;
    }

    @Override
    public String Ztjk(String expCode, String expNo) {
        String str = "";
        try {
            str = ztjkApi.getOrderTracesByJson(expCode, expNo);
            System.out.println("返回信息：：：：：" + str);
        } catch (Exception ec) {
            System.out.println(ec.getStackTrace());
        }
        return str;
    }

    @Override
    public String cloudPrint(String expCode, String expNo, Integer isPreview) {
        String str = "";
        try {
            str = cloudPrintApi.sendPrintData(expCode, expNo, isPreview);
            System.out.println("返回信息：：：：：" + str);
        } catch (Exception ec) {
            System.out.println(ec.getStackTrace());
        }
        return str;
    }

    @Override
    public PrintParams getCloudPrintParams(String ip, String printerName, String expCode, String expNo, Integer isPreview) {
        try {
            return cloudPrintApi.getPrintParam(ip, expCode, expNo, printerName, isPreview);
        } catch (Exception ec) {
            System.out.println(ec.getStackTrace());
        }
        return null;
    }
}